package com.wyzz.hr.util.comparator;

import java.util.Comparator;
import java.util.List;

public class ListComparator extends IComparator implements Comparator {

	public ListComparator() {
	}

	public ListComparator(int orderArr[], int compareMethod[]) {
		setOrderArr(orderArr);
		if (compareMethod.length > 0)
			setCompareMethod(compareMethod[0]);
	}

	public ListComparator(int orderArr[][], int compareMethod[]) {
		setOrderArr(orderArr);
		if (compareMethod.length > 0)
			setCompareMethod(compareMethod[0]);
	}

	public int compare(List list1, List list2) {
		for (int i = 0; i < orderArr.length; i++) {
			if (objComparator[i] == null)
				objComparator[i] = new ObjectComparator(orderArr[i][1],
						new int[0]);
			int resultTmp = objComparator[i].compare(list1.get(orderArr[i][0]),
					list2.get(orderArr[i][0]));
			if (resultTmp != 0)
				return resultTmp;
		}

		return 0;
	}

	public int[][] getOrderArr() {
		return orderArr;
	}

	public void setOrderArr(int orderArr[]) {
		this.orderArr = new int[orderArr.length][2];
		for (int i = 0; i < orderArr.length; i++) {
			this.orderArr[i][0] = orderArr[i];
			this.orderArr[i][1] = 1;
		}

		objComparator = new ObjectComparator[this.orderArr.length];
	}

	public void setOrderArr(int orderArr[][]) {
		this.orderArr = orderArr;
		objComparator = new ObjectComparator[this.orderArr.length];
	}

	public int compare(Object x0, Object x1) {
		return compare((List) x0, (List) x1);
	}

	private int orderArr[][];
	private ObjectComparator objComparator[];
}
